	var My_widget;
	if (!My_widget) My_widget = {};
	
	/*
	 * upload module
	 */
	My_widget.upload = {};
	
	My_widget.upload.upload_widget = function(options) {
		$("#operation_modal .modal-body").html(
			'<div class="upload-widget">' +
				'<div class="preview">' +
					'<img>' +
				'</div>' +
				'<div class="upload">' +
					'<div id="dropbox" class="droparea">' +
						'<div class="instructions">请将尺寸为<b>' + options['width'] + 'x' + options['height'] + '</b>的图片拖放至此</div>' +
						'<div class="progress progress-info progress-striped active" style="visibility: hidden;">' +
							'<div class="bar" style="width: 0%;"></div>' +
						'</div>' +
					'</div>' +
				'</div>' +
			'</div>'
		);
		
		$("#dropbox").css({ width: options['width'], height: options['height'] });
		
		if (options['widget_id'] != undefined) {
			$(".upload-widget").attr('id', options['widget_id']);
		}
		if (options['widget_class'] != undefined) {
			$(".upload-widget").addClass(options['widget_class']);
		}
		if (options['src_path'] != undefined) {
			$(".upload-widget .preview img").attr('src', options['src_path']);
		}
		if (options['readme'] != undefined) {
			$("#operation_modal .modal-header h3").append(options['readme']);
		}
		
		My_widget.upload.to_upload_img = document.createElement("img");
		
		var this_post_url = null;
		if (controller == 'project' || controller == 'service' || controller == 'media') {
			this_post_url = base_url + "index.php/backend/" + controller + "/detail/" + type + "/" + selector_replace(current_selector) + "/" + current_operate + "/true";
		} else {
			this_post_url = base_url + "index.php/backend/" + controller + "/" + type + "/" + type + "/" + selector_replace(current_selector) + "/" + current_operate + "/true";
		}
		
		$("#dropbox").html5Uploader({
			postUrl: this_post_url,
			onClientLoad: function(e, file) {
				My_widget.upload.to_upload_img.src = e.target.result;
				var width = My_widget.upload.to_upload_img.width;
	            var height = My_widget.upload.to_upload_img.height;
	            if (width != width || height != height) {
					return false;
				}
			},
			onServerLoadStart: function(e, file) {
				My_widget.upload.showProgressBar();
				$(".upload").find(".bar").css({ 'width': '0%' });
			},
			onServerProgress: function(e, file) {
				if(e.lengthComputable) {
					var percentComplete = (e.loaded / e.total) * 100;
					$(".upload").find(".bar").css({ 'width': percentComplete + '%' });
				}
			},
			onServerLoad: function(e, file) {
				$(".upload").find(".bar").css({ 'width': '100%' });
			},
			onSuccess: function(e, file) {
				setTimeout("My_widget.upload.hideProgressBar();My_widget.upload.showUploadPic();", 1000);
			}
		});
	}

	My_widget.upload.showProgressBar = function() {
		$(".upload").find(".progress").css({ 'visibility': 'visible' });
	}
	
	My_widget.upload.hideProgressBar = function() {
		$(".upload").find(".progress").css({ 'visibility': 'hidden' }).find(".bar").css({ 'width': '0%' });
	}
	
	My_widget.upload.showUploadPic = function() {
		$(".preview img").attr('src', My_widget.upload.to_upload_img.src).effect('slide', '', effect_speed);
		reload_page();
	}
	
	/*
	 * sortable module
	 */
	My_widget.sortable = {};
	
	My_widget.sortable.sortable_widget = function(options) {
		$sortable_widget = $(".sortable-widget");
		$sortable = $(".sortable-widget .sortable");
		
		My_widget.sortable.sortable_obj = $sortable;
		
		if (options['trash'] == true) {
			$trash = $(".sortable-widget .trash");
			My_widget.sortable.trash_obj = $trash;
		}
		if (options['editable'] == true) {
			My_widget.sortable.editable();
		}
		if (options['resize_times'] == undefined) {
			options['resize_times'] = 1;
		}
		if (options['readme'] != undefined) {
			$("#operation_modal .modal-header h3").append(options['readme']);
		}
		
		My_widget.sortable.options = options;
		switch (options['type']) {
			case 'image':
				$sortable.find("li").css({ width: parseInt(options['width'] / options['resize_times']) });
				$sortable.find("li img").css({ width: parseInt(options['width'] / options['resize_times']), height: parseInt(options['height'] / options['resize_times']) });
				break;
			case 'text':
				break;
			default:
				return false;
		}
		
		$sortable.find("ul").sortable({
			handle: 'i.icon-move',
			containment: '.sortable',
			revert: 'invalid',
			update: function() {
				My_widget.sortable.do_sort_update_post();
			}
		});
		
		$('#operation_modal').on('shown', function () {
			$sortable_widget.height($sortable.height());
			$sortable.height($sortable.height());
			
			if (options['trash'] == true) {
				$trash.height($sortable.height());
			}
		});
		
		if (options['trash'] == true) {
			My_widget.sortable.trash(options);
		}
	}
	
	My_widget.sortable.do_sort_update_post = function() {
		set_ajax_post_attributes(true);
		
		post_option[My_widget.sortable.options['inject_param']] = My_widget.sortable.sortable_obj.find("ul").sortable('widget').sortable('toArray');
		do_ajax_post();
	}
	
	My_widget.sortable.editable = function() {
		$sortable.find("li").append('<i class="icon-edit"></i><i class="icon-check" style="display: none;"></i>');
		                  
		var orig_text = null;
		$("i.icon-edit").die().live('click', function() {
			if ($(this).parent().siblings("li").children("div").find("input").length != 0) {
				return;
			}
			orig_text = $(this).siblings("div").html();
			
			$(this).siblings("div").html('<input value="' + orig_text + '" style="margin-top: -4px;height: 12px;">');
			
			$(this).css('display', 'none').siblings("i.icon-check").css('display', 'inline-block');
		});
		$("i.icon-check").die().live('click', function() {
			var mod_text = $(this).siblings("div").children("input").attr('value');
			$(this).siblings("div").html(mod_text);
			
			$(this).css('display', 'none').siblings("i.icon-edit").css('display', 'inline-block');
			
			if (mod_text != orig_text) {
				set_ajax_post_attributes(true);
				
				post_option['operate'] = 'edit_text';
				post_option['mod_id'] = $(this).parent("li").attr('id');
				post_option['mod_text'] = mod_text;
				
				inject_exec = '$(".sortable-widget .sortable").find("li:eq(0)").attr(\'id\', data[\'id\'])';
				do_ajax_post(inject_exec);
			}
		});
		$(".btn.create").click(function() {
			if ($sortable.find("input").length == 0) {
				$sortable.find("ul").prepend(
					'<li>' +
						'<div><input style="margin-top: -4px;height: 12px;"></div>' +
						'<i class="icon-move"></i>' +
						'<i class="icon-edit" style="display: none;"></i><i class="icon-check"></i>' +
					'</li>'
				);
			} else {
				alert('还有正在编辑的条目');
			}
		});
	}
	
	My_widget.sortable.trash = function(options) {
		$sortable = $(".sortable-widget .sortable");
		$trash = $(".sortable-widget .trash");
		
		$sortable.find("li").liveDraggable({
			cancel: 'i, div > input',
			revert: 'invalid',
			containment: '.sortable-widget',
			helper: 'clone',
		});

		$sortable.find("li i.icon-move").mousedown(function() {
			$sortable.find("ul").sortable("enable");
			$sortable.find("li").draggable("disable");
		});
		
		$sortable.find("li").children().not("i.icon-move").mousedown(function() {
			$sortable.find("ul").sortable("disable");
			$sortable.find("li").draggable("enable");
		});
		
		$trash.liveDroppable({
			hoverClass: 'hover',
			accept: '.sortable li',
			drop: function(event, ui) {
				deleteItem(ui.draggable);
			}
		});

		$sortable.liveDroppable({
			accept: '.trash li',
			drop: function(event, ui) {
				recycleItem(ui.draggable);
			}
		});
		
		function deleteItem($item) {
			$item.find("i").css({ 'display': 'none' });
			$item.fadeOut(function() {
				var $list = $("ul", $trash).length ? $("ul", $trash) : $("<ul/>").appendTo($trash);

				$item.appendTo($list).fadeIn(function() {
					$item.animate({ width: options['width'] / (options['resize_times'] * 2) }).find("img").animate({ height: options['height'] / (options['resize_times'] * 2) });
				});
			});
			
			My_widget.sortable.do_trash_update_post($item.filter("li").attr('id'), true);
		}
		
		function recycleItem($item) {
			$item.find("i").not("i.icon-check").css({ 'display': 'inline-block' });
			$item.fadeOut(function() {
				$item.css("width", parseInt(options['width'] / options['resize_times'])).find("img").css("height", parseInt(options['height'] / options['resize_times'])).end().appendTo($(".sortable ul")).fadeIn();
				My_widget.sortable.do_sort_update_post();
			});
			
			My_widget.sortable.do_trash_update_post($item.filter("li").attr('id'), false);
		}
	}
	
	My_widget.sortable.do_trash_update_post = function(id, is_delete) {
		set_ajax_post_attributes(true);
		
		post_option['operate'] = 'delete';
		post_option['id'] = id;
		post_option['is_delete'] = is_delete;
		
		do_ajax_post();
	}
	
	
	
	
	